'use strict';
	angular.module('invoice.invoiceSelectMulti', [])
	.controller('invoiceSelectMultiCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {


		var Model = this;


		Model.gridRowClick = function(event){
			
		}

		Model.gridRowDblClick = function(event){
			Model.ok();
		}
		
		
		var options = {};
		options.httpUrl = $rootScope.getServiceName();
		options.reader = '/invoice/accountInvoice/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},orderType:{name:"orderType",type:"String",label:""},orderTypeLabel:{name:"orderTypeLabel",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},audit:{name:"audit",type:"String",label:""},flowType:{name:"flowType",type:"Integer",label:""},revOpay:{name:"revOpay",type:"String",label:""},direction:{name:"direction",type:"Integer",label:""},confirmDate:{name:"confirmDate",type:"Date",label:""},origin:{name:"origin",type:"String",label:""},originName:{name:"originName",type:"String",label:""},originNo:{name:"originNo",type:"String",label:""},periodStatus:{name:"periodStatus",type:"String",label:""},periodNo:{name:"periodNo",type:"String",label:""},accountInvoiceNo:{name:"accountInvoiceNo",type:"String",label:""},invoiceCategory:{name:"invoiceCategory",type:"String",label:""},invoiceCategoryLabel:{name:"invoiceCategoryLabel",type:"String",label:""},invoiceCode:{name:"invoiceCode",type:"String",label:""},invoiceDate:{name:"invoiceDate",type:"Date",label:""},dealObject:{name:"dealObject",type:"String",label:""},dealObjectLabel:{name:"dealObjectLabel",type:"String",label:""},customerID:{name:"customerID",type:"String",label:""},customerNo:{name:"customerNo",type:"String",label:""},shortName:{name:"shortName",type:"String",label:""},customerName:{name:"customerName",type:"String",label:""},taxType:{name:"taxType",type:"Integer",label:""},taxTypeLabel:{name:"taxTypeLabel",type:"String",label:""},taxRate:{name:"taxRate",type:"Decimal",label:""},fPhone:{name:"fPhone",type:"String",label:""},taxNo:{name:"taxNo",type:"String",label:""},accountBank:{name:"accountBank",type:"String",label:""},accountNo:{name:"accountNo",type:"String",label:""},captionID:{name:"captionID",type:"String",label:""},captionNo:{name:"captionNo",type:"String",label:""},captionName:{name:"captionName",type:"String",label:""},captionFullName:{name:"captionFullName",type:"String",label:""},itemAmount:{name:"itemAmount",type:"Decimal",label:""},orderAmount:{name:"orderAmount",type:"Decimal",label:""},orderAmountOld:{name:"orderAmountOld",type:"Decimal",label:""},orderAmountTax:{name:"orderAmountTax",type:"Decimal",label:""},orderAmountTotal:{name:"orderAmountTotal",type:"Decimal",label:""},orderAmountCleared:{name:"orderAmountCleared",type:"Decimal",label:""},pendingAmount:{name:"pendingAmount",type:"Decimal",label:""},isMember:{name:"isMember",type:"Integer",label:""},memberNo:{name:"memberNo",type:"String",label:""},isHaveCoupon:{name:"isHaveCoupon",type:"Integer",label:""},discountCash:{name:"discountCash",type:"Decimal",label:""},depositRate:{name:"depositRate",type:"Decimal",label:""},accountAmountOld:{name:"accountAmountOld",type:"Decimal",label:""},accountAmountTax:{name:"accountAmountTax",type:"Decimal",label:""},accountAmountTotal:{name:"accountAmountTotal",type:"Decimal",label:""},discountRate:{name:"discountRate",type:"Decimal",label:""},fMasterID:{name:"fMasterID",type:"String",label:""},paymentTerm:{name:"paymentTerm",type:"String",label:""},paymentTermLabel:{name:"paymentTermLabel",type:"String",label:""},predictStart:{name:"predictStart",type:"Date",label:""},predictEnd:{name:"predictEnd",type:"Date",label:""},cashSettle:{name:"cashSettle",type:"Integer",label:""},next:{name:"next",type:"String",label:""},nextName:{name:"nextName",type:"String",label:""},nextNo:{name:"nextNo",type:"String",label:""},settleCategory:{name:"settleCategory",type:"String",label:""},settleCategoryLabel:{name:"settleCategoryLabel",type:"String",label:""},orderNo:{name:"orderNo",type:"String",label:""},projectNo:{name:"projectNo",type:"String",label:""},serialNumber:{name:"serialNumber",type:"String",label:""},resultStatus:{name:"resultStatus",type:"String",label:""},handling:{name:"handling",type:"String",label:""},handlingLabel:{name:"handlingLabel",type:"String",label:""},clearDatetime:{name:"clearDatetime",type:"Datetime",label:""},detailsCount:{name:"detailsCount",type:"Integer",label:""},detailsCountState:{name:"detailsCountState",type:"Integer",label:""},maxDetailNo:{name:"maxDetailNo",type:"Integer",label:""},employeeNo:{name:"employeeNo",type:"String",label:""},employeeName:{name:"employeeName",type:"String",label:""},comments:{name:"comments",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},tUsercode:{name:"tUsercode",type:"String",label:""},tUsername:{name:"tUsername",type:"String",label:""},tDatetime:{name:"tDatetime",type:"Datetime",label:""},eUsercode:{name:"eUsercode",type:"String",label:""},eUsername:{name:"eUsername",type:"String",label:""},eDatetime:{name:"eDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = 'fID';
		options.directDeleteMode = true;
		options.limit = 10;
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataListIndexChanged;
		options.onBeforeNew = Model.dataListBeforeNew;
		options.onAfterNew = Model.dataListAfterNew;
		options.onBeforeDelete = Model.dataListBeforeDelete;
		options.onAfterDelete = Model.dataListAfterDelete;
		options.onValueChanged = Model.dataListValueChanged;
		options.onDataChanged = Model.dataListDataChanged;
		
		$scope.dataList = new BizData(options);

		//监视对象中的所有变动
//		$scope.dataList.deregRows = $scope.$watch('dataList.rows',$scope.dataList.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		
		
		////////////////////启动执行开始///////////////////////////////////////////////////
		//接收调用的参数
//		Model.params = $rootScope.$stateParams;  
		

		////////////////////启动执行结束///////////////////////////////////////////////////
			

		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
        	Model.title = title;  
        	Model.params = params;  

    		//设置过滤条件
    		$scope.dataList.filter = "AccountInvoice.status='open' "
    		+" AND AccountInvoice.audit='confirm' "
//    		+" AND AccountInvoice.pendingAmount>0 "
    		;
    		
//    		console.log('Model.params.filter-->',Model.params.filter);
    		if(Model.params.filter!=undefined && Model.params.filter!=''){
    			$scope.dataList.filter = $scope.dataList.filter + " AND " + Model.params.filter;
    		}

    		//刷新数据
    	    $scope.dataList.refreshData();
        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
	    	for(var index=0;index<$scope.dataList.rows.length;index++){
	    		if($scope.dataList.rows[index].userdata.checked){
	    	    	
	    	    	var sourceRow = $scope.dataList.rows[index];
	    	    	var row = {};
	    			//循环行中的列，取出列的值，装配成简单的行
	    			for(var obj in sourceRow){
	    				if(obj!='$$hashKey' && obj!='userdata'){
	    					row[obj] = sourceRow[obj].value;
	    				}
	    			}
	    	    	
	    	    	data.push(row);
	    			
	    		}
	    	}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    

	  //表格标题中的选择框ng-model
	  $scope.checkboxValue = false;
	  //表格标题中的选择对话框click事件
	  Model.checkboxClick = function () {  
	  	for(var index=0;index<$scope.dataList.rows.length;index++){
	  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
	  	}
	  };    

	  //////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
